﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VersionComparer.Base;
using VersionComparer.DataModel.Assemblies.Images;

namespace VersionComparer.DataModel.Assemblies
{
    [Serializable]
    [Order(3)]
    public class EnumItem : TypeItem
    {
        public override string TypeType
        {
            get
            {
                return "enum";
            }
        }

        protected override void Initialize(Type value)
        {
            base.Initialize(value);
            Text = value.Name;
        }

        protected override string GetFullText(Type value)
        {
            var rtf = Rtf;

            BuildAttributesRtf(rtf, false, false);
            rtf.Append(string.Format ("{0}{1} " , AccessorsAsCode, TypeType), keywordColor);
            rtf.AppendLine(Name, nameColor);

            rtf.Append("{");
            rtf.Ident ();
            
            StringBuilder code = new StringBuilder();
            string[] names = Enum.GetNames(value);
            Array values = Enum.GetValues(value);
            Type destinationType = Enum.GetUnderlyingType(value);
            for (int i = 0; i < names.Length; i++)
            {
                if (i > 0)
                    rtf.Append ( ",");
                rtf.AppendLine();
                rtf.Append ( string.Format ( "{0} = " , names[i]));
                BuildValueRtf ( destinationType , Convert.ChangeType(values.GetValue(i), destinationType), rtf);
            }
            rtf.DeIdent ();
            rtf.AppendLine();
            rtf.AppendLine("}");
            return rtf.Text;
        }


        protected override void BuildRtf(RtfBuilder builder, bool differencesOnly, bool useParentColor)
        {
            builder.Append(Rtf);
        }
        protected override ComparableItem[] GetChilds(Type value)
        {
            return new ComparableItem[0];
        }


        protected override System.Drawing.Image GetSpecificImage()
        {
            return AssemblyResources._enum;
        }
    }
}
